/**
 * 路由事件工具函数
 * 提供触发路由相关事件的便捷方法
 */

/**
 * 触发用户权限变化事件
 * 当用户权限发生变化时调用此函数
 */
export const triggerUserPermissionChanged = () => {
  const event = new CustomEvent('userPermissionChanged', {
    detail: {
      timestamp: Date.now(),
      source: 'manual'
    }
  });
  window.dispatchEvent(event);
  console.log('已触发用户权限变化事件');
};

/**
 * 触发菜单更新事件
 * 当菜单配置发生变化时调用此函数
 */
export const triggerMenuUpdated = () => {
  const event = new CustomEvent('menuUpdated', {
    detail: {
      timestamp: Date.now(),
      source: 'manual'
    }
  });
  window.dispatchEvent(event);
  console.log('已触发菜单更新事件');
};

/**
 * 触发用户角色变化事件
 * 当用户角色发生变化时调用此函数
 */
export const triggerUserRoleChanged = () => {
  const event = new CustomEvent('userRoleChanged', {
    detail: {
      timestamp: Date.now(),
      source: 'manual'
    }
  });
  window.dispatchEvent(event);
  console.log('已触发用户角色变化事件');
};

/**
 * 触发登录状态变化事件
 * 当用户登录或登出时调用此函数
 */
export const triggerLoginStateChanged = () => {
  const event = new CustomEvent('loginStateChanged', {
    detail: {
      timestamp: Date.now(),
      source: 'manual'
    }
  });
  window.dispatchEvent(event);
  console.log('已触发登录状态变化事件');
};

/**
 * 批量触发所有路由相关事件
 * 用于测试或强制刷新所有路由相关状态
 */
export const triggerAllRouteEvents = () => {
  triggerUserPermissionChanged();
  triggerMenuUpdated();
  triggerUserRoleChanged();
  triggerLoginStateChanged();
  console.log('已触发所有路由相关事件');
};

/**
 * 路由事件类型定义
 */
export type RouteEventType = 
  | 'userPermissionChanged'
  | 'menuUpdated'
  | 'userRoleChanged'
  | 'loginStateChanged';

/**
 * 监听路由事件的通用函数
 * @param eventType 事件类型
 * @param callback 回调函数
 * @returns 取消监听的函数
 */
export const listenRouteEvent = (
  eventType: RouteEventType,
  callback: (event: CustomEvent) => void
) => {
  window.addEventListener(eventType, callback as EventListener);
  
  return () => {
    window.removeEventListener(eventType, callback as EventListener);
  };
};

/**
 * 路由事件管理器
 * 提供更高级的事件管理功能
 */
export class RouteEventManager {
  private listeners: Map<RouteEventType, Set<(event: CustomEvent) => void>> = new Map();

  /**
   * 添加事件监听器
   */
  on(eventType: RouteEventType, callback: (event: CustomEvent) => void) {
    if (!this.listeners.has(eventType)) {
      this.listeners.set(eventType, new Set());
    }
    this.listeners.get(eventType)!.add(callback);
    window.addEventListener(eventType, callback as EventListener);
  }

  /**
   * 移除事件监听器
   */
  off(eventType: RouteEventType, callback: (event: CustomEvent) => void) {
    const callbacks = this.listeners.get(eventType);
    if (callbacks) {
      callbacks.delete(callback);
      window.removeEventListener(eventType, callback as EventListener);
    }
  }

  /**
   * 触发事件
   */
  emit(eventType: RouteEventType, detail?: any) {
    const event = new CustomEvent(eventType, {
      detail: {
        timestamp: Date.now(),
        source: 'RouteEventManager',
        ...detail
      }
    });
    window.dispatchEvent(event);
  }

  /**
   * 清理所有监听器
   */
  cleanup() {
    for (const [eventType, callbacks] of this.listeners) {
      for (const callback of callbacks) {
        window.removeEventListener(eventType, callback as EventListener);
      }
    }
    this.listeners.clear();
  }

  /**
   * 获取当前监听器数量
   */
  getListenerCount(eventType?: RouteEventType): number {
    if (eventType) {
      return this.listeners.get(eventType)?.size || 0;
    }
    return Array.from(this.listeners.values()).reduce((total, set) => total + set.size, 0);
  }
}

// 导出单例实例
export const routeEventManager = new RouteEventManager();